package com.dzf.service.impl;

import com.dzf.entity.*;
import com.dzf.mapper.OrdMapper;
import com.dzf.service.OrdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrdServiceImpl implements OrdService {
    @Autowired
    OrdMapper ordMapper;

    @Override
    public List<ItemBook> findItemInfoByOrdid(int ordid) {
        return ordMapper.findItemInfoByOrdid(ordid);
    }

    @Override
    public void addShip(Order ord) {
        ordMapper.addShip(ord);
    }

    @Transactional
    @Override
    public Map<String,Object> findOrdInfo(Map<String, Object> map) {
        //将结果封装好
        Map<String,Object> resultmap =  new HashMap<>();
        List<OrderLine> orderLines = ordMapper.findOrdInfo(map);
        resultmap.put("total",ordMapper.findByTotal(map));//总行
        resultmap.put("orders",orderLines);
        return resultmap;
    }

    @Override
    public int findByTotal(Map<String, Object> map) {
        return ordMapper.findByTotal(map);
    }

    @Override
    public void addAddr(Order ord) {
        ordMapper.addAddr(ord);
    }

    @Override
    public void addOrd(Order order) {
        ordMapper.addOrd(order);
    }

    @Transactional
    @Override
    public OrdInfo addOrdItem(MyUser user,Order order,List<Cart> carts) {
//      订单头添加
        this.addOrd(order);

        Map<String,Object> map = new HashMap<>();
        map.put("ordid",order.getOrdid());
        map.put("carts",carts);
        //订单详细添加
        ordMapper.addOrdItem(map);


        Map<String,Object> map2 = new HashMap<>();
        System.err.println(order.getOrdid());
        map2.put("ordid",order.getOrdid());
        map2.put("uid",user.getId());
        OrdInfo ordinfo = ordMapper.findInfoByOrdid(map2);//将订单头和订单详细返回
        return ordinfo;//xml中selectkey，所以添加后的订单头id会保存在对象中，查询后返回出去让前端可以拿到数据
    }
    public OrdInfo findInfoByOrdid(int uid,int ordid)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("uid",uid);
        map.put("ordid",ordid);
        return ordMapper.findInfoByOrdid(map);
    }

//   不分页的订单查询
    @Override
    public List<OrderLine> findOrdLines(Map<String, Object> map) {
        return ordMapper.findOrdLines(map);
    }

    @Override
    public void addPay(Order ord) {
        ordMapper.addPay(ord);
    }
}
